<template>
  <div>
    <el-form :model="operationHistory" :rules="dataRule" ref="dataForm" label-width="120px">
      <el-form-item label="手术名称" prop="operationName">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4}"
          show-word-limit
          maxlength="100"
          v-model="operationHistory.operationName"
          placeholder="请输入手术名称"
        ></el-input>
      </el-form-item>

      <el-form-item label="治疗单位" prop="treatmentUnit">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4}"
          show-word-limit
          maxlength="100"
          v-model="operationHistory.treatmentUnit"
          placeholder="请输入治疗单位/操作者"
        ></el-input>
      </el-form-item>

      <el-form-item label="手术日期" prop="operationTime">
        <el-date-picker
          style="width:320px;"
          v-model="operationTime"
          @change="operationTimeChange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  components: {},
  props: {},
  data() {
    return {
      operationTime: [],
      operationHistory: {
        operationName: "",
        treatmentUnit: "",
        startTime: null,
        endTime: null
      },
      dataRule: {
        operationName: [
          {
            required: true,
            message: "手术名称必须填写",
            trigger: "blur"
          }
        ],
        treatmentUnit: [
          {
            required: true,
            message: "治疗单位/操作者必须填写",
            trigger: "blur"
          }
        ],
        operationTime: [
          {
            validator: (rule, value, callback) => {
              if (
                !this.operationHistory.startTime ||
                !this.operationHistory.endTime
              ) {
                callback(new Error("药物使用时间必须选择"));
              } else {
                callback();
              }
            },
            required: true,
            trigger: ["blur", "change"]
          }
        ]
      }
    };
  },
  methods: {
    putOperationHistory(value) {
      this.operationHistory = value;
      this.operationTime.push(value.startTime);
      this.operationTime.push(value.endTime);
    },
    operationTimeChange(value) {
      if (value != null && value.length == 2) {
        this.operationHistory.startTime = value[0];
        this.operationHistory.endTime = value[1];
      } else if (value == null) {
        this.operationHistory.startTime = null;
        this.operationHistory.endTime = null;
        this.operationTime = null;
      }
    },
    submit(value) {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          // 调用父组件的赋值方法
          if (value == "koaOperationHistory") {
            // 如果父组件传来的是‘koaDrugHistory’，说明是koa
            this.operationHistory.isKoa = 1;
          } else {
            this.operationHistory.isKoa = 0;
          }
        }
      });
      if (
        this.operationHistory.isKoa == 0 ||
        this.operationHistory.isKoa == 1
      ) {
        return this.operationHistory;
      } else {
        return null;
      }
    }
  }
};
</script>
